home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Sound / PreludeAMP / src / misc2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-25  |  24.0 KB  |  802 lines

  1. /* this file is a part of amp software, (C) tomislav uzelac 1996,1997
  2. */
  3.  
  4. /* misc2.c  requantization, stereo processing, reordering(shortbl) and antialiasing butterflies
  5.  *
  6.  * misc.c was created by tomislav uzelac in May 1996, and was completely awful
  7.  * Created by: tomislav uzelac Dec 22 1996
  8.  * some more speed injected, cca. Jun 1 1997
  9.  */
  10. #include <math.h>
  11.  
  12. #include "amp.h"
  13. #include "audio.h"
  14. #include "getdata.h"
  15. #include "huffman.h"
  16.  
  17. #define MISC2
  18. #include "misc2.h"
  19.  
  20. /* 
  21.  * fras == Formula for Requantization and All Scaling **************************
  22.  */
  23. static inline float fras_l(int sfb,int global_gain,int scalefac_scale,int scalefac,int preflag)
  24. {
  25. register int a,scale;
  26.     /*
  27.         if (scalefac_scale) scale=2;
  28.     else scale=1;
  29.     */
  30.     scale=scalefac_scale+1;
  31.     a= global_gain -210 -(scalefac << scale);
  32.         if (preflag) a-=(t_pretab[sfb] << scale);
  33.  
  34. /* bugfix, Mar 13 97: shifting won't produce a legal result if we shift by more than 31
  35.  * since global_gain<256, this can only occur for (very) negative values of a.
  36. */
  37.     if (a < -127) return 0;
  38.  
  39. /* a minor change here as well, no point in abs() if we now that a<0
  40. */
  41.         if (a>=0) return tab[a&3]*(1 << (a>>2));
  42.     else return tabi[(-a)&3]/(1 << ((-a) >> 2));
  43. }
  44.  
  45. static inline float fras_s(int global_gain,int subblock_gain,int scalefac_scale,int scalefac)
  46. {
  47. int a;
  48.         a=global_gain - 210 - (subblock_gain << 3);
  49.     if (scalefac_scale) a-= (scalefac << 2);
  50.     else a-= (scalefac << 1);
  51.  
  52.         if (a < -127) return 0;
  53.  
  54.         if (a>=0) return tab[a&3]*(1 << (a>>2));
  55.         else return tabi[(-a)&3]/(1 << ((-a) >> 2));
  56. }
  57.  
  58. /* this should be faster than pow()
  59.  */
  60. static inline float fras2(int is,float a)
  61. {
  62.         if (is > 0) return t_43[is]*a;
  63.         else return -t_43[-is]*a;
  64. }
  65.  
  66. /*
  67.  * requantize_mono *************************************************************
  68.  */
  69.  
  70. /* generally, the two channels do not have to be of the same block type - that's why we do two passes with requantize_mono.
  71.  * if ms or intensity stereo is enabled we do a single pass with requantize_ms because both channels are same block type
  72.  */
  73.  
  74. void requantize_mono(int gr,int ch,struct SIDE_INFO *info,struct AUDIO_HEADER *header)
  75. {
  76. int l,i,sfb;
  77. float a;
  78. int global_gain=info->global_gain[gr][ch];
  79. int scalefac_scale=info->scalefac_scale[gr][ch];
  80. int sfreq=header->sampling_frequency;
  81.  
  82.  
  83.     no_of_imdcts[0]=no_of_imdcts[1]=32;
  84.  
  85.     if (info->window_switching_flag[gr][ch] && info->block_type[gr][ch]==2)    
  86.         if (info->mixed_block_flag[gr][ch]) {
  87.     /*
  88.      * requantize_mono - mixed blocks/long block part **********************
  89.      */
  90.                     int window,window_len,preflag=0; /* pretab is all zero in this low frequency area */
  91.                     int scalefac=scalefac_l[gr][ch][0];
  92.  
  93.             l=0;sfb=0;
  94.             a=fras_l(sfb,global_gain,scalefac_scale,scalefac,preflag);
  95.                     while (l<36) {
  96.                             xr[ch][0][l]=fras2(is[ch][l],a);
  97.                             if (l==t_l[sfb]) {
  98.                     scalefac=scalefac_l[gr][ch][++sfb];
  99.                     a=fras_l(sfb,global_gain,scalefac_scale,scalefac,preflag);
  100.                 }
  101.                             l++;
  102.                     }
  103.     /*
  104.      * requantize_mono - mixed blocks/short block part *********************
  105.      */
  106.             sfb=3; 
  107.             window_len=t_s[sfb]-t_s[sfb-1];
  108.                     while (l<non_zero[ch]) {
  109.                         for (window=0;window<3;window++) {
  110.                                 int scalefac=scalefac_s[gr][ch][sfb][window];
  111.                                     int subblock_gain=info->subblock_gain[gr][ch][window];
  112.                     a=fras_s(global_gain,subblock_gain,scalefac_scale,scalefac);
  113.                                     for (i=0;i<window_len;i++) {
  114.                                               xr[ch][0][t_reorder[header->ID][sfreq][l]]=fras2(is[ch][l],a);
  115.                                         l++;
  116.                                            }
  117.                                    }
  118.                                    sfb++;
  119.                                    window_len=t_s[sfb]-t_s[sfb-1];
  120.             }
  121.             while (l<576) xr[ch][0][t_reorder[header->ID][sfreq][l++]]=0;
  122.         } else {
  123.     /*
  124.      * requantize mono - short blocks **************************************
  125.      */
  126.             int window,window_len;
  127.  
  128.             sfb=0; l=0;
  129.             window_len=t_s[0]+1;
  130.             while (l<non_zero[ch]) {
  131.                 for (window=0;window<3;window++) {
  132.                     int scalefac=scalefac_s[gr][ch][sfb][window];
  133.                     int subblock_gain=info->subblock_gain[gr][ch][window];
  134.                     float a=fras_s(global_gain,subblock_gain,scalefac_scale,scalefac);
  135.                     for (i=0;i<window_len;i++) {
  136.                         xr[ch][0][t_reorder[header->ID][sfreq][l]]=fras2(is[ch][l],a);
  137.                         l++;
  138.                     }
  139.                 }
  140.                 sfb++;
  141.                 window_len=t_s[sfb]-t_s[sfb-1];
  142.             }
  143.             while (l<576) xr[ch][0][t_reorder[header->ID][sfreq][l++]]=0;
  144.         }
  145.     else {
  146.     /* long blocks */
  147.         int preflag=info->preflag[gr][ch];
  148.         int scalefac=scalefac_l[gr][ch][0];
  149.  
  150.         sfb=0; l=0;
  151.         a=fras_l(sfb,global_gain,scalefac_scale,scalefac,preflag);
  152.         while (l<non_zero[ch]) {
  153.             xr[ch][0][l]=fras2(is[ch][l],a); 
  154.             if (l==t_l[sfb]) {
  155.                 scalefac=scalefac_l[gr][ch][++sfb];
  156.                 a=fras_l(sfb,global_gain,scalefac_scale,scalefac,preflag);
  157.             }
  158.             l++;
  159.         }        
  160.         while (l<576) xr[ch][0][l++]=0;
  161.     }
  162. }
  163.  
  164. /*
  165.  * stereo stuff ****************************************************************
  166.  */
  167. static int find_isbound(int isbound[3],int gr,struct SIDE_INFO *info,struct AUDIO_HEADER *header)
  168. {
  169. int sfb,window,window_len,ms_flag,tmp,i;
  170.  
  171.     isbound[0]=isbound[1]=isbound[2]=-1;
  172.     no_of_imdcts[0]=no_of_imdcts[1]=32;
  173.  
  174.    if (header->mode_extension==1 || header->mode_extension==3) {
  175.     if (info->window_switching_flag[gr][0] && info->block_type[gr][0]==2) {
  176.  
  177.     /* find that isbound!
  178.      */
  179.         tmp=non_zero[1];
  180.         sfb=0; while ((3*t_s[sfb]+2) < tmp  && sfb < 12) sfb++;
  181.         while ((isbound[0]<0 || isbound[1]<0 || isbound[2]<0) && !(info->mixed_block_flag[gr][0] && sfb<3) && sfb) {
  182.             for (window=0;window<3;window++) {
  183.                 if (sfb==0) {
  184.                     window_len=t_s[0]+1;
  185.                     tmp=(window+1)*window_len - 1;
  186.                 } else {
  187.                     window_len=t_s[sfb]-t_s[sfb-1];
  188.                     tmp=(3*t_s[sfb-1]+2) + (window+1)*window_len;
  189.                 }
  190.                 if (isbound[window] < 0)
  191.                     for (i=0;i<window_len;i++)
  192.                         if (is[1][tmp--] != 0) {
  193.                             isbound[window]=t_s[sfb]+1; 
  194.                             break;
  195.                         }
  196.             }
  197.             sfb--;
  198.         }
  199.     
  200.     /* mixed block magic now...
  201.      */
  202.         if (sfb==2 && info->mixed_block_flag[gr][0]) 
  203.             if (isbound[0]<0 && isbound[1]<0 && isbound[2]<0) {
  204.                 tmp=35;
  205.                 while (is[1][tmp] == 0) tmp--;
  206.                 sfb=0; while (t_l[sfb] < tmp  && sfb < 21) sfb++;
  207.                 isbound[0]=isbound[1]=isbound[2]=t_l[sfb]+1;
  208.             } else for (window=0;window<3;window++) 
  209.                 if (isbound[window]<0) isbound[window]=36;
  210.         if (header->ID==1) isbound[0]=isbound[1]=isbound[2]=MAX(isbound[0],MAX(isbound[1],isbound[2]));
  211.  
  212.     /* just how many imdcts?
  213.      */
  214.         tmp=non_zero[0];
  215.         sfb=0; while ((3*t_s[sfb]+2) < tmp && sfb < 12) sfb++;
  216.         no_of_imdcts[0]=no_of_imdcts[1]=(t_s[sfb]-1)/6+1; 
  217.     } else {
  218.  
  219.     /* long blocks now
  220.      */
  221.                 tmp=non_zero[1];
  222.                 while (is[1][tmp] == 0) tmp--;
  223.                 sfb=0; while (t_l[sfb] < tmp && sfb < 21) sfb++;
  224.         isbound[0]=isbound[1]=isbound[2]=t_l[sfb]+1;
  225.         no_of_imdcts[0]=no_of_imdcts[1]=(non_zero[0]-1)/18+1; 
  226.     }
  227.     if (header->mode_extension==1) ms_flag=0;
  228.     else ms_flag=1;
  229.    } else {
  230.  
  231.    /* intensity stereo is, regretably, turned off
  232.     */
  233.     ms_flag=1;
  234.  
  235.     /* i really put a lot of work in this, but it still looks like shit (works, though)
  236.      */ 
  237.     if (!info->window_switching_flag[gr][0] || (info->window_switching_flag[gr][0] && info->block_type[gr][0]!=2)) 
  238.         isbound[0]=isbound[1]=isbound[2]=(MAX(non_zero[0],non_zero[1]));
  239.     else isbound[0]=isbound[1]=isbound[2]=576;
  240.  
  241.     if (info->window_switching_flag[gr][0] && info->block_type[gr][0]==2) {
  242.         /* should do for mixed blocks too, though i havent tested... */
  243.             tmp=(MAX(non_zero[0],non_zero[1]))/3;
  244.             sfb=0; while (t_s[sfb]<tmp && sfb<12) sfb++;
  245.             no_of_imdcts[0]=no_of_imdcts[1]=(t_s[sfb]-1)/6+1;
  246.     }
  247.     else no_of_imdcts[0]=no_of_imdcts[1]=(isbound[0]-1)/18+1;
  248.  
  249.    }
  250.  
  251.    return ms_flag;
  252. }
  253.  
  254. static inline void stereo_s(int l,float a[2],int pos,int ms_flag,int is_pos,struct AUDIO_HEADER *header)
  255. {
  256. float ftmp,Mi,Si;
  257.  
  258.     if (l>=576) return; /* brrr... */
  259.  
  260.         if ((is_pos != IS_ILLEGAL) && (header->ID==1)) {
  261.                 ftmp=fras2(is[0][l],a[0]);
  262.                 xr[0][0][pos]=(1-t_is[is_pos])*ftmp;
  263.                 xr[1][0][pos]=t_is[is_pos]*ftmp;
  264.         return;
  265.     }
  266.  
  267.     if ((is_pos != IS_ILLEGAL) && (header->ID==0)) {
  268.         ftmp=fras2(is[0][l],a[0]);
  269.         if (is_pos&1) {
  270.             xr[0][0][pos]= t_is2[intensity_scale][(is_pos+1)>>1] * ftmp;
  271.             xr[1][0][pos]= ftmp;
  272.         } else {
  273.             xr[0][0][pos]= ftmp;
  274.             xr[1][0][pos]= t_is2[intensity_scale][is_pos>>1] * ftmp;
  275.         }
  276.         return;
  277.     }
  278.  
  279.         if (ms_flag) {
  280.                 Mi=fras2(is[0][l],a[0]);
  281.                 Si=fras2(is[1][l],a[1]);
  282.                 xr[0][0][pos]=(Mi+Si)*i_sq2;
  283.                 xr[1][0][pos]=(Mi-Si)*i_sq2;
  284.         } else {
  285.                 xr[0][0][pos]=fras2(is[0][l],a[0]);
  286.                 xr[1][0][pos]=fras2(is[1][l],a[1]);
  287.         }
  288. }
  289.  
  290. static inline void stereo_l(int l,float a[2],int ms_flag,int is_pos,struct AUDIO_HEADER *header)
  291. {
  292. float ftmp,Mi,Si;
  293.     if (l>=576) return;
  294.  
  295. /* new code by ???
  296. */
  297.     if (is_pos != IS_ILLEGAL) {
  298.         ftmp = fras2(is[0][l], a[0]);
  299.         if (header -> ID ==1) {
  300.             xr[0][0][l] = (1 - t_is[is_pos]) * ftmp;
  301.             xr[1][0][l] = t_is[is_pos] * ftmp;
  302.             return;
  303.         } else if (is_pos & 1) {
  304.             xr[0][0][l] = t_is2[intensity_scale][(is_pos + 1) >> 1] * ftmp;
  305.             xr[1][0][l] = ftmp;
  306.         } else {
  307.             xr[0][0][l] = ftmp;
  308.             xr[1][0][l] = t_is2[intensity_scale][is_pos >> 1] * ftmp;
  309.         }
  310.         return;
  311.     }
  312.  
  313.     if (ms_flag) {
  314.         Mi=fras2(is[0][l],a[0]);
  315.         Si=fras2(is[1][l],a[1]);
  316.         xr[0][0][l]=(Mi+Si)*i_sq2;
  317.         xr[1][0][l]=(Mi-Si)*i_sq2;
  318.     } else {
  319.         xr[0][0][l]=fras2(is[0][l],a[0]);
  320.         xr[1][0][l]=fras2(is[1][l],a[1]);
  321.     }
  322.  
  323. }
  324.  
  325.  
  326. /*
  327.  * requantize_ms ***************************************************************
  328.  */
  329. void requantize_ms(int gr,struct SIDE_INFO *info,struct AUDIO_HEADER *header)
  330. {
  331. int l,sfb,ms_flag,is_pos,i,ch;
  332. int *global_gain,subblock_gain[2],*scalefac_scale,scalefac[2],isbound[3];
  333. int sfreq=header->sampling_frequency;
  334. int id = header->ID;
  335. float a[2];
  336.  
  337. global_gain=info->global_gain[gr];
  338. scalefac_scale=info->scalefac_scale[gr];
  339.  
  340.         if (info->window_switching_flag[gr][0] && info->block_type[gr][0]==2)  
  341.                 if (info->mixed_block_flag[gr][0]) {
  342.     /* 
  343.      * mixed blocks w/stereo processing - long block part ******************
  344.      */
  345.                         int window,window_len;
  346.                         int preflag[2]={0,0};
  347.  
  348.             ms_flag=find_isbound(isbound,gr,info,header);
  349.  
  350.             sfb=0; l=0;
  351.             for (ch=0;ch<2;ch++) {
  352.                 scalefac[ch]=scalefac_l[gr][ch][0];
  353.                 a[ch]=fras_l(0,global_gain[ch],scalefac_scale[ch],scalefac[ch],preflag[ch]);
  354.             }
  355.  
  356.  
  357.             while (l<36) {
  358.                 int is_pos;
  359.                 if (l<isbound[0]) is_pos=IS_ILLEGAL;
  360.                 else {
  361.                     is_pos=scalefac[1];
  362.                     if (id==1) /* MPEG1 */
  363.                         if (is_pos==7) is_pos=IS_ILLEGAL;
  364.                     else /* MPEG2 */
  365.                         if (is_pos==is_max[sfb]) is_pos=IS_ILLEGAL;
  366.                 }
  367.  
  368.                 stereo_l(l,a,ms_flag,is_pos,header);
  369.  
  370.                 if (l==t_l[sfb]) {
  371.                     sfb++;
  372.                     for (ch=0;ch<2;ch++) {
  373.                         scalefac[ch]=scalefac_l[gr][ch][sfb];
  374.                         a[ch]=fras_l(sfb,global_gain[ch],scalefac_scale[ch],scalefac[ch],preflag[ch]);
  375.                     }
  376.                 }
  377.  
  378.                 l++;
  379.             }
  380.     /*
  381.      * mixed blocks w/stereo processing - short block part *****************
  382.      */
  383.                         sfb=3;
  384.                         window_len=t_s[sfb]-t_s[sfb-1];
  385.  
  386.                         while (l<(MAX(non_zero[0],non_zero[1]))) {
  387.                                 for (window=0;window<3;window++) {
  388.                                         subblock_gain[0]=info->subblock_gain[gr][0][window];
  389.                     subblock_gain[1]=info->subblock_gain[gr][1][window];
  390.                     scalefac[0]=scalefac_s[gr][0][sfb][window];
  391.                     scalefac[1]=scalefac_s[gr][1][sfb][window];
  392.  
  393.                     if (t_s[sfb] < isbound[window]) {
  394.                         is_pos=IS_ILLEGAL;
  395.                         a[0]=fras_s(global_gain[0],subblock_gain[0],scalefac_scale[0],scalefac[0]);
  396.                         a[1]=fras_s(global_gain[1],subblock_gain[1],scalefac_scale[1],scalefac[1]);
  397.                     } else {
  398.                         is_pos=scalefac[1];
  399.                         if (id==1) /* MPEG1 */
  400.                             if (is_pos==7) is_pos=IS_ILLEGAL;
  401.                         else /* MPEG2 */
  402.                             if (is_pos==is_max[sfb+6]) is_pos=IS_ILLEGAL;
  403.  
  404.                         a[0]=fras_s(global_gain[0],subblock_gain[0],scalefac_scale[0],scalefac[0]);
  405.                     }
  406.  
  407.                                         for (i=0;i<window_len;i++) {
  408.                                                 stereo_s(l,a,t_reorder[id][sfreq][l],ms_flag,is_pos,header);
  409.                                                 l++;
  410.                                         }
  411.                                 }
  412.                                 sfb++;
  413.                                 window_len=t_s[sfb]-t_s[sfb-1];
  414.                         }
  415.                         while (l<576) {
  416.                     int reorder = t_reorder[id][sfreq][l++];
  417.               
  418.                 xr[0][0][reorder]=xr[1][0][reorder]=0;
  419.             }
  420.                 } else {                                                                       
  421.         /*
  422.      * requantize_ms - short blocks w/stereo processing ********************
  423.      */
  424.                         int window,window_len;
  425.  
  426.             ms_flag=find_isbound(isbound,gr,info,header);    
  427.             sfb=0; l=0;
  428.             window_len=t_s[0]+1;
  429.  
  430.                         while (l<(MAX(non_zero[0],non_zero[1]))) {
  431.                                 for (window=0;window<3;window++) {
  432.                     subblock_gain[0]=info->subblock_gain[gr][0][window];
  433.                     subblock_gain[1]=info->subblock_gain[gr][1][window];
  434.                     scalefac[0]=scalefac_s[gr][0][sfb][window];
  435.                     scalefac[1]=scalefac_s[gr][1][sfb][window];
  436.  
  437.                                         if (t_s[sfb] < isbound[window]) {
  438.                                                 is_pos=IS_ILLEGAL;
  439.                         a[0]=fras_s(global_gain[0],subblock_gain[0],scalefac_scale[0],scalefac[0]);
  440.                         a[1]=fras_s(global_gain[1],subblock_gain[1],scalefac_scale[1],scalefac[1]);
  441.                                         } else {
  442.                                                 is_pos=scalefac[1];
  443.                                                 if (id==1) /* MPEG1 */
  444.                                                         if (is_pos==7) is_pos=IS_ILLEGAL;
  445.                                                 else /* MPEG2 */
  446.                                                         if (is_pos==is_max[sfb+6]) is_pos=IS_ILLEGAL;
  447.  
  448.                                                 a[0]=fras_s(global_gain[0],subblock_gain[0],scalefac_scale[0],scalefac[0]);
  449.                                         }
  450.  
  451.                                         for (i=0;i<window_len;i++) {
  452.                         stereo_s(l,a,t_reorder[id][sfreq][l],ms_flag,is_pos,header);
  453.                         l++;
  454.                     }
  455.                                 }
  456.                                 /* this won't do anymore
  457.                  * window_len=-t_s[sfb]+t_s[++sfb];
  458.                  */
  459.                 window_len  = -t_s[sfb++];
  460.                 window_len +=  t_s[sfb];
  461.  
  462.                         }
  463.                         while (l<576) {
  464.                     int reorder = t_reorder[id][sfreq][l++];
  465.               
  466.                 xr[0][0][reorder]=xr[1][0][reorder]=0;
  467.             }
  468.                 }
  469.         else {
  470.         /*
  471.      * long blocks w/stereo processing *************************************
  472.      */
  473.                 int *preflag=info->preflag[gr];
  474.  
  475.         ms_flag=find_isbound(isbound,gr,info,header);
  476.  
  477.         sfb=0; l=0;
  478.         scalefac[0]=scalefac_l[gr][0][sfb];
  479.         a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
  480.         scalefac[1]=scalefac_l[gr][1][sfb];
  481.         a[1]=fras_l(sfb,global_gain[1],scalefac_scale[1],scalefac[1],preflag[1]);
  482.  
  483.         /* no intensity stereo part
  484.         */
  485.     if (ms_flag)
  486.                 while (l< isbound[0]) {
  487. #if defined(PENTIUM_RDTSC)
  488.  
  489. unsigned int cnt4, cnt3, cnt2, cnt1;
  490. static int min_cycles = 99999999;
  491.  
  492.         __asm__(".byte 0x0f,0x31" : "=a" (cnt1), "=d" (cnt4));
  493. #endif
  494.  
  495.             {
  496.                 register float Mi = fras2(is[0][l],a[0]);
  497.                 register float Si = fras2(is[1][l],a[1]);
  498.                 register float tmp = i_sq2;
  499.                 xr[0][0][l]=(Mi+Si)*tmp;
  500.                 xr[1][0][l]=(Mi-Si)*tmp;
  501.             }
  502.  
  503. #if defined(PENTIUM_RDTSC)
  504.                         __asm__(".byte 0x0f,0x31" : "=a" (cnt2), "=d" (cnt4));
  505.  
  506.                         if (cnt2-cnt1 < min_cycles) {
  507.                           min_cycles = cnt2-cnt1;
  508.                           printf("%d cycles\n", min_cycles);
  509.                         }
  510.  
  511. #endif
  512.                         if (l==t_l[sfb]) {
  513. #if defined(PENTIUM_RDTSC2)
  514.  
  515. unsigned int cnt4, cnt2, cnt1;
  516. static int min_cycles = 99999999;
  517.  
  518.         __asm__(".byte 0x0f,0x31" : "=a" (cnt1), "=d" (cnt4));
  519. #endif
  520.  
  521.                                 sfb++;
  522.                 scalefac[0]=scalefac_l[gr][0][sfb];
  523.                 a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
  524.                 scalefac[1]=scalefac_l[gr][1][sfb];
  525.                 a[1]=fras_l(sfb,global_gain[1],scalefac_scale[1],scalefac[1],preflag[1]);
  526. #if defined(PENTIUM_RDTSC2)
  527.                         __asm__(".byte 0x0f,0x31" : "=a" (cnt2), "=d" (cnt4));
  528.  
  529.                         if (cnt2-cnt1 < min_cycles) {
  530.                           min_cycles = cnt2-cnt1;
  531.                           printf("%d cycles, sfb %d\n", min_cycles, sfb);
  532.                         }
  533.  
  534. #endif
  535.                         }
  536.                         l++;
  537.                 }
  538.     else
  539.                 while (l< isbound[0]) {
  540.                         xr[0][0][l]=fras2(is[0][l],a[0]);
  541.                         xr[1][0][l]=fras2(is[1][l],a[1]);
  542.                         if (l==t_l[sfb]) {
  543.                                 sfb++;
  544.                                 scalefac[0]=scalefac_l[gr][0][sfb];
  545.                                 a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
  546.                                 scalefac[1]=scalefac_l[gr][1][sfb];
  547.                                 a[1]=fras_l(sfb,global_gain[1],scalefac_scale[1],scalefac[1],preflag[1]);
  548.                         }
  549.                         l++;
  550.                 }
  551.  
  552.  
  553.         /* intensity stereo part
  554.         */
  555.         while (l<(MAX(non_zero[0],non_zero[1]))) {
  556.             int is_pos=scalefac[1];
  557.     
  558.             if (id==1) /* MPEG1 */
  559.                 if (is_pos==7) is_pos=IS_ILLEGAL;
  560.             else /* MPEG2 */
  561.                 if (is_pos==is_max[sfb]) is_pos=IS_ILLEGAL;
  562.  
  563.             stereo_l(l,a,ms_flag,is_pos,header);
  564.  
  565.             if (l==t_l[sfb]) {
  566.                 sfb++;
  567.                 scalefac[0]=scalefac_l[gr][0][sfb];
  568.                 scalefac[1]=scalefac_l[gr][1][sfb];
  569.                 a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
  570.             }
  571.             l++;
  572.         }
  573.  
  574.                 while (l<576) {
  575.             xr[0][0][l]=0;
  576.             xr[1][0][l]=0;
  577.             l++;
  578.         }
  579.         }
  580. }
  581.  
  582. /*
  583.  * requantize_downmix **********************************************************
  584.  */
  585. void requantize_downmix(int gr,struct SIDE_INFO *info,struct AUDIO_HEADER *header)
  586. {
  587. int l,sfb,ms_flag,is_pos,i,ch;
  588. int *global_gain,subblock_gain[2],*scalefac_scale,scalefac[2],isbound[3];
  589. int sfreq=header->sampling_frequency;
  590. int id = header->ID;
  591. float a[2];
  592.  
  593.     /* first set some variables
  594.     */
  595.     global_gain=info->global_gain[gr];
  596.     scalefac_scale=info->scalefac_scale[gr];
  597.  
  598.     if (header->mode_extension==2 || header->mode_extension==3) ms_flag=1;
  599.     else ms_flag=0;
  600.  
  601.     /* ... and then we're off for requantization
  602.     */
  603.     if (info->window_switching_flag[gr][0] && info->block_type[gr][0]==2)
  604.         if (info->mixed_block_flag[gr][0]) {
  605.             die("mixed block? hmmmm...., downmix for mixed blocks is not yet implemented\n");
  606.         } else {
  607.             int window,window_len;
  608.             int isbound[3];
  609.             int is_pos;
  610.             int dummy=find_isbound(isbound,gr,info,header); /* ugly hack, part3 */
  611.  
  612.             sfb=0; l=0; window_len=t_s[0]+1;
  613.             
  614.             while (l<(MAX(non_zero[0],non_zero[1]))) {
  615.                 for (window=0;window<3;window++) {
  616.                     subblock_gain[0]=info->subblock_gain[gr][0][window];
  617.                     subblock_gain[1]=info->subblock_gain[gr][1][window];
  618.                     scalefac[0]=scalefac_s[gr][0][sfb][window];
  619.                     is_pos=scalefac[1]=scalefac_s[gr][1][sfb][window];
  620.  
  621.                     if (t_s[sfb] < isbound[window]) {
  622.                         a[0]=fras_s(global_gain[0],subblock_gain[0],scalefac_scale[0],scalefac[0]);
  623.                         if (ms_flag) {
  624.                             for (i=0;i<window_len;i++) {
  625.                                 register float Mi=fras2(is[0][l],a[0]);
  626.                                 register float tmp = i_sq2;
  627.                                 xr[0][0][t_reorder[id][sfreq][l]]=Mi*i_sq2;
  628.                                 l++;
  629.                             }
  630.                         } else {
  631.                             a[1]=fras_s(global_gain[1],subblock_gain[1],scalefac_scale[1],scalefac[1]);
  632.                             for (i=0;i<window_len;i++) {
  633.                                 register float tmp1=fras2(is[0][l],a[0]);
  634.                                 register float tmp2=fras2(is[1][l],a[1]);
  635.                                 xr[0][0][t_reorder[id][sfreq][l]]=(tmp1+tmp2)*0.5f;
  636.                                 l++;
  637.                             }
  638.                         }
  639.                     } else {
  640.                         a[0]=fras_s(global_gain[0],subblock_gain[0],scalefac_scale[0],scalefac[0]);
  641.                         for (i=0;i<window_len;i++) {
  642.                             register float ftmp = fras2(is[0][l], a[0]);
  643.                             if (id==0 && is_pos<is_max[sfb])
  644.                                 ftmp*=t_downmix[intensity_scale][(is_pos+1)>>1];
  645.                             xr[0][0][t_reorder[id][sfreq][l]] = ftmp;
  646.                             l++;
  647.                         }                        
  648.                     }
  649.                 }
  650.                 window_len  = -t_s[sfb++];
  651.                 window_len +=  t_s[sfb];
  652.             }
  653.             while (l<576) {
  654.                 xr[0][0][l]=0;
  655.                 l++;
  656.             }
  657.         }
  658.     else {
  659.         int *preflag=info->preflag[gr];
  660.         int isbound;
  661.  
  662.         if (header->mode_extension==1 || header->mode_extension==3) {
  663.             int tmp=non_zero[1];
  664.             while (is[1][tmp] == 0) tmp--;
  665.             sfb=0; while (t_l[sfb] < tmp && sfb < 21) sfb++;
  666.             isbound=t_l[sfb]+1;
  667.             no_of_imdcts[0]=no_of_imdcts[1]=(non_zero[0]-1)/18+1;
  668.         } else {
  669.             isbound=(MAX(non_zero[0],non_zero[1]));
  670.             no_of_imdcts[0]=no_of_imdcts[1]=(isbound-1)/18+1;
  671.         }
  672.  
  673.                 sfb=0; l=0;
  674.                 scalefac[0]=scalefac_l[gr][0][sfb];
  675.                 a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
  676.                 scalefac[1]=scalefac_l[gr][1][sfb];
  677.                 a[1]=fras_l(sfb,global_gain[1],scalefac_scale[1],scalefac[1],preflag[1]);        
  678.  
  679.         /* no intensity stereo part
  680.          */
  681.         if (ms_flag)
  682.             while (l < isbound) {
  683.                 register float Mi = fras2(is[0][l],a[0]);
  684.                 register float tmp = i_sq2;
  685.                 xr[0][0][l]=Mi*tmp;
  686.  
  687.                 if (l==t_l[sfb]) {
  688.                     sfb++;
  689.                                     scalefac[0]=scalefac_l[gr][0][sfb];
  690.                                     a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
  691.                     scalefac[1]=scalefac_l[gr][1][sfb];
  692.                 }
  693.                 l++;
  694.             }
  695.         else 
  696.             while (l < isbound) {
  697.                 register float tmp1=fras2(is[0][l],a[0]);
  698.                 register float tmp2=fras2(is[1][l],a[1]);
  699.                 xr[0][0][l]=(tmp1+tmp2)*0.5f;
  700.                             if (l==t_l[sfb]) {
  701.                                     sfb++;
  702.                                     scalefac[0]=scalefac_l[gr][0][sfb];
  703.                                     a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
  704.                                     scalefac[1]=scalefac_l[gr][1][sfb];
  705.                                     a[1]=fras_l(sfb,global_gain[1],scalefac_scale[1],scalefac[1],preflag[1]);
  706.                             }
  707.                             l++;
  708.             }
  709.         /* intensity stereo part
  710.         */
  711.         while (l<(MAX(non_zero[0],non_zero[1]))) {
  712.             int is_pos=scalefac[1];
  713.             register float ftmp=fras2(is[0][l], a[0]);
  714.  
  715.             if (id==0  && is_pos<is_max[sfb]) {
  716.                 ftmp*=t_downmix[intensity_scale][(is_pos+1)>>1];
  717.             }
  718.  
  719.             xr[0][0][l] = ftmp;
  720.  
  721.             if (l==t_l[sfb]) {
  722.                 sfb++;
  723.                 scalefac[0]=scalefac_l[gr][0][sfb];
  724.                 a[0]=fras_l(sfb,global_gain[0],scalefac_scale[0],scalefac[0],preflag[0]);
  725.                 scalefac[1]=scalefac_l[gr][1][sfb];
  726.             }
  727.             l++;
  728.         }
  729.  
  730.         /* _always_ zero out everything else
  731.         */
  732.         while (l<576) {
  733.             xr[0][0][l]=0;
  734.             l++;
  735.         }
  736.     }
  737. }
  738.  
  739. /* 
  740.  * antialiasing butterflies ****************************************************
  741.  * 
  742.  */
  743. void alias_reduction(int ch)
  744. {
  745. unsigned int sb;
  746.  
  747.         for (sb=1;sb<32;sb++) {
  748.             float *x = xr[ch][sb];
  749.         register float a, b;
  750.  
  751.             a = x[0];
  752.         b = x[-1];
  753.                 x[-1] = b * Cs[0] - a * Ca[0];
  754.                 x[0]  = a * Cs[0] + b * Ca[0];
  755.  
  756.             a = x[1];
  757.         b = x[-2];
  758.                 x[-2] = b * Cs[1] - a * Ca[1];
  759.                 x[1]  = a * Cs[1] + b * Ca[1];
  760.  
  761.             a = x[2];
  762.         b = x[-3];
  763.                 x[-3] = b * Cs[2] - a * Ca[2];
  764.                 x[2]  = a * Cs[2] + b * Ca[2];
  765.  
  766.             a = x[3];
  767.         b = x[-4];
  768.                 x[-4] = b * Cs[3] - a * Ca[3];
  769.                 x[3]  = a * Cs[3] + b * Ca[3];
  770.  
  771.             a = x[4];
  772.         b = x[-5];
  773.                 x[-5] = b * Cs[4] - a * Ca[4];
  774.                 x[4]  = a * Cs[4] + b * Ca[4];
  775.  
  776.             a = x[5];
  777.         b = x[-6];
  778.                 x[-6] = b * Cs[5] - a * Ca[5];
  779.                 x[5]  = a * Cs[5] + b * Ca[5];
  780.  
  781.             a = x[6];
  782.         b = x[-7];
  783.                 x[-7] = b * Cs[6] - a * Ca[6];
  784.                 x[6]  = a * Cs[6] + b * Ca[6];
  785.  
  786.             a = x[7];
  787.         b = x[-8];
  788.                 x[-8] = b * Cs[7] - a * Ca[7];
  789.                 x[7]  = a * Cs[7] + b * Ca[7];
  790.     }
  791. }
  792.  
  793. /* calculating t_43 instead of having that big table in misc2.h
  794.  */
  795.  
  796. void calculate_t43(void)
  797. {
  798. int i;
  799.     for (i=0;i<8192;i++)
  800.         t_43[i]=(float)pow((float)i,1.33333333333);
  801. }
  802.